<?php

    require_once($application_folder."/controllers/navigator.php");
    class Login extends navigator {

        function login() {
            parent:: navigator();
            $this->load->library('form_validation');
        }

        function index() {
            $this->load->view('login_v');
        }

        function prijava(){
            if($this->valid()) { 
                // attempt to login
                if($this->simple_login_secure->login($this->input->post('email'), $this->input->post('password'))==TRUE) {
                    // success
                    redirect(base_url().'anketa/uredi_anketu/list_polls');
                    
                    echo $this->session->userdata('id');
                    echo $this->session->userdata('email');
                    echo $this->session->userdata('password');
                    echo $this->session->userdata('comapny');
                    echo $this->session->userdata('city');
                    echo $this->session->userdata('zip');
                    echo $this->session->userdata('firstname');
                    echo $this->session->userdata('lastname'); 
                    
                    
                }else {
                    $data['poruka'] = 'Molimo unesite ispravne pristupne podatke';
                    $this->load->view('login_v',$data);
                }
            }else {
                $this->load->view('login_v');
            }
        }

        function dologin() {

            if($this->input->post('remember')) {
                $islog = $this->dx_auth->login($this->input->post('email'), $this->input->post('password'),TRUE);
            }else {
                $islog = $this->dx_auth->login($this->input->post('email'), $this->input->post('password'));
            }

            if($islog) {
                $this->_setAgency($this->input->post('email'));
                // sada salji na dashboard ako je agencija ili na listu agencija ako je administrator
                if($this->dx_auth->get_role_name() == 'user') {
                    redirect(base_url().'prequest/views');
                }elseif($this->dx_auth->get_role_name() == 'admin') {
                    redirect(base_url().'agency/views');
                }
            }else {
                $this->load->view('administracija/login_v');
            }

        }

        function logout() {         
            // logout
            $this->simple_login_secure->logout();
            $this->index();
        }

        function valid() {
            $this->form_validation->set_rules('email','Email adresa','trim|xss_clean|required|valid_email');
            $this->form_validation->set_rules('password','Lozinka','trim|xss_clean|required');
            return $this->form_validation->run();
        }

        function _setAgency($email) {
            if($this->dx_auth->get_role_name() == 'user') {
                $user = $this->db->where('email', $email)->get('users')->row_array();
                $agency = $this->db->where('user_id', $user['id'])->get('agencies')->row_array();
                $this->session->set_userdata('is_agency', 1);
                $this->session->set_userdata('agency_id', $agency['id']);
            }
        }

    }
?>